package com.dogkr.server.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.dogkr.common.Constants;
import com.dogkr.common.ServerAction;
import com.dogkr.server.entity.Host;
import com.dogkr.server.entity.HostPhoto;
import com.dogkr.server.entity.HostPhotoAlbum;
import com.dogkr.server.entity.PetServer;
import com.dogkr.server.entity.User;
import com.dogkr.server.entity.UserPet;
import com.dogkr.server.service.PetServerService;
import com.dogkr.server.service.PhotoService;
import com.dogkr.server.service.UserPetService;
import com.dogkr.server.service.UserService;
import com.sean.common.utils.DateUtils;


// 设置Struts2注解
@ParentPackage(value = "login-default")
@Namespace(value = "/frame")
@ResultPath("/WEB-INF/jsps")
// 页面文件空间
@Results({
	@Result(name = "Main", location = "main.jsp"),
	@Result(name = "Order", location = "order.jsp"),
	@Result(name = "Quote", location = "quote.jsp"),
	@Result(name = "Msg", location = "msg.jsp"),
	@Result(name = "Favorites", location = "fvt.jsp"),
	@Result(name = "Host", location = "host.jsp"),
	@Result(name = "Server", location = "server.jsp"),
	@Result(name = "Info", location = "info.jsp"),
	@Result(name = "PetMain", location = "pet_main.jsp"),
	@Result(name = "Manage", location = "manage.jsp"),
	@Result(name = "SevDate", location = "server_date.jsp"),
	@Result(name = "PhotoMain", location = "photo_main.jsp")
})
public class FrameworkAction extends ServerAction {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private PetServerService petServerService;
	
	@Autowired
	private UserPetService userPetService;
	
	@Autowired
	private PhotoService photoService;
	
	@Action(value = "main")
	public String main() {
		return "Main";
	}
	
	@Action(value = "msg")
	public String msg() {
		return "Msg";
	}
	
	@Action(value="favorites")
	public String favorites(){
		return "Favorites";
	}
	@Action(value = "host")
	public String host() {
		return "Host";
	}
	@Action(value = "order")
	public String order() {
		return "Order";
	}
	@Action(value = "quote")
	public String quote() {
		return "Quote";
	}
	@Action(value = "info")
	public String info() {
		return "Info";
	}
	@Action(value = "pet")
	public String petMain() {
		User user = this.getLoginUser();
		List<UserPet> list = userPetService.selectUserPetByUserKey(user.getId());
		put("petList", list);
		return "PetMain";
	}
	
	@Action(value = "photo")
	public String photoMain() {
		Host host = this.getLoginUser().getHost();
		List<HostPhoto> photoList = photoService.selectPhotoByHostKey(host.getId());
		List<HostPhotoAlbum> albumList = photoService.selectPhotoAlbumByHostKey(host.getId());
		put("photoList", photoList);
		put("albumList", albumList);
		put("fileSize", Constants.UPLOAD_FILE_SIZE);
		return "PhotoMain";
	}
	
	@Action(value = "manage")
	public String manageMain() {
		return "Manage";
	}
	
	@Action(value = "sevDate")
	public String sevDate() {
		Date searchDate = null;
		try {
			searchDate = DateUtils.parse(this.get("date"), "yyyy-MM");
		} catch (Exception e) {}
		if(searchDate == null) {
			searchDate = new Date();
		}
		List<List<Date>> calendarList = DateUtils.getCalendar(searchDate);
		put("searchDate", searchDate);
		put("today", new Date());
		put("calendar", calendarList);
		put("next", DateUtils.addMonths(searchDate, 1));
		put("back", DateUtils.addMonths(searchDate, -1));
		return "SevDate";
	}

	@Action(value = "server")
	public String server() {
		List<PetServer> sysServerList = petServerService.selectSysPetServerWithAll();
		put("sysServerList", sysServerList);
		return "Server";
	}
}
